package com.isoft.service.impl;

import com.isoft.entity.DfmeSflgEntity;
import com.isoft.service.XmlFileHandler;
import com.isoft.util.XmlParserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;

@Component
public class DfmeSflgFileHandler implements XmlFileHandler {
    private static final Logger logger = LoggerFactory.getLogger(DfmeSflgFileHandler.class);

    @Autowired
    private DfmeSflgServiceImpl dfmeSflgService;
    @Autowired
    private XmlParserUtil xmlParserUtil;

    @Override
    public boolean supports(Path filePath) {
        return filePath != null && filePath.getFileName().toString().toUpperCase().contains("DFME-SFLG");
    }

    @Override
    public String handle(Path filePath) throws Exception {
        try {
            logger.info("开始处理DFME-SFLG文件: {}", filePath.getFileName());
            String xmlContent = Files.readString(filePath);
            DfmeSflgEntity entity = xmlParserUtil.parseXml(xmlContent, DfmeSflgEntity.class);
            logger.debug("解析结果: {}", entity);
            dfmeSflgService.parseAndSave(entity);
            return "success";
        } catch (Exception e) {
            logger.error("处理DFME-SFLG文件失败: {}", filePath.getFileName(), e);
            throw new RuntimeException("Failed to process DFME-SFLG XML: " + e.getMessage(), e);
        }
    }
} 